﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Tasks
{
    public class MergeJSTask
    {

        public static void Execute(string src)
        {
            src.EnsureEndsWith("\\");
            GenerateGeneral(src);
            GenerateForDNN6(src);
        }

        private static void GenerateGeneral(string src)
        {
            var dir = new DirectoryInfo(src);

            var completeFramework = new StringBuilder();
            foreach (var subDir in dir.GetDirectories())
            {
                var result = ProcessDirectory(src, subDir, "", null).ToString();
                if (result.Length == 0) continue;
                completeFramework.AppendLine("// BEGIN DIRECTORY: " + subDir.Name);
                completeFramework.Append(result); 
                completeFramework.AppendLine();
                completeFramework.AppendLine("// END DIRECTORY: " + subDir.Name);
                completeFramework.AppendLine();
                
            }
            File.WriteAllText(src + "framework.all.min.js",  completeFramework.ToString());
        }

        private static void GenerateForDNN6(string src)
        {
            var dir = new DirectoryInfo(src);

            var exclude = new List<string>();
            exclude.Add("00.01.jquery-1.6.2.min.js");
            exclude.Add("01.01.jquery-ui-1.8.14.min.js");
            exclude.Add("00.03.json2.min.js");

            var completeFramework = new StringBuilder();
            foreach (var subDir in dir.GetDirectories())
            {
                var result = ProcessDirectory(src, subDir, "dnn6", exclude).ToString();
                if (result.Length == 0) continue;
                completeFramework.AppendLine("// BEGIN DIRECTORY: " + subDir.Name);
                completeFramework.Append(result);
                completeFramework.AppendLine();
                completeFramework.AppendLine("// END DIRECTORY: " + subDir.Name);
                completeFramework.AppendLine();

            }
            File.WriteAllText(src + "framework.all.dnn6.min.js", completeFramework.ToString());
        }



        private static StringBuilder ProcessDirectory(string rootSrc, DirectoryInfo dir, string fileNamePrefix, List<string> excludeFiles)
        {
            fileNamePrefix = string.IsNullOrEmpty(fileNamePrefix) ? "" : "." + fileNamePrefix;

            if (excludeFiles == null) excludeFiles = new List<string>();
            var framework = new StringBuilder();
            foreach (var f in dir.GetFiles())
            {
                var skip = excludeFiles.Any(ef => f.Name.ToLower() == ef.ToLower());

                if (skip || !f.Name.EndsWith("min.js")) continue;

                framework.AppendLine("// BEGIN FILE: " + f.Name);
                framework.Append(File.ReadAllText(f.FullName));
                framework.AppendLine();
                framework.AppendLine("// END FILE: " + f.Name);
                framework.AppendLine();

            }

            // Do not write if nothing is included
            if (framework.Length > 0) 
                File.WriteAllText(rootSrc + "framework." + dir.Name + fileNamePrefix + ".min.js", framework.ToString());

            return framework;
        }

    }
}
